package com.ruoyi.web.controller.mch;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.mch.vo.WxRedEnvelopeUrlVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mch.vo.WxRedEnvelopeVo;
import com.ruoyi.mch.bo.WxRedEnvelopeQueryBo;
import com.ruoyi.mch.bo.WxRedEnvelopeAddBo;
import com.ruoyi.mch.bo.WxRedEnvelopeEditBo;
import com.ruoyi.mch.service.IWxRedEnvelopeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 红包Controller
 *
 * @author ruoyi
 * @date 2021-06-11
 */
@Api(value = "红包控制器", tags = {"红包管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mch/envelope")
public class WxRedEnvelopeController extends BaseController {

    private final IWxRedEnvelopeService iWxRedEnvelopeService;

    /**
     * 查询红包列表
     */
    @ApiOperation("查询红包列表")
    @PreAuthorize("@ss.hasPermi('mch:envelope:list')")
    @GetMapping("/list")
    public TableDataInfo<WxRedEnvelopeVo> list(WxRedEnvelopeQueryBo bo) {
        startPage();
        List<WxRedEnvelopeVo> list = iWxRedEnvelopeService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出红包列表
     */
    @ApiOperation("导出红包列表")
    @PreAuthorize("@ss.hasPermi('mch:envelope:export')")
    @Log(title = "红包", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<WxRedEnvelopeVo> export(WxRedEnvelopeQueryBo bo) {
        List<WxRedEnvelopeVo> list = iWxRedEnvelopeService.queryList(bo);
        ExcelUtil<WxRedEnvelopeVo> util = new ExcelUtil<WxRedEnvelopeVo>(WxRedEnvelopeVo.class);
        return util.exportExcel(list, "红包");
    }

    /**
     * 获取红包详细信息
     */
    @ApiOperation("获取红包详细信息")
    @PreAuthorize("@ss.hasPermi('mch:envelope:query')")
    @GetMapping("/{reId}")
    public AjaxResult<WxRedEnvelopeVo> getInfo(@PathVariable("reId" ) String reId) {
        return AjaxResult.success(iWxRedEnvelopeService.queryById(reId));
    }

    /**
     * 新增红包
     */
    @ApiOperation("新增红包")
    @PreAuthorize("@ss.hasPermi('mch:envelope:add')")
    @Log(title = "红包", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody WxRedEnvelopeAddBo bo) {
        return toAjax(iWxRedEnvelopeService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改红包
     */
    @ApiOperation("修改红包")
    @PreAuthorize("@ss.hasPermi('mch:envelope:edit')")
    @Log(title = "红包", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody WxRedEnvelopeEditBo bo) {
        return toAjax(iWxRedEnvelopeService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除红包
     */
    @ApiOperation("删除红包")
    @PreAuthorize("@ss.hasPermi('mch:envelope:remove')")
    @Log(title = "红包" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{reIds}")
    public AjaxResult<Void> remove(@PathVariable String[] reIds) {
        return toAjax(iWxRedEnvelopeService.deleteWithValidByIds(Arrays.asList(reIds), true) ? 1 : 0);
    }

    /**
     * 获取红包链接
     */
    @ApiOperation("获取红包链接")
    @GetMapping("/reUrl")
    public AjaxResult<WxRedEnvelopeUrlVo> getReUrl(String configId,Double amount) {
        WxRedEnvelopeUrlVo vo = new WxRedEnvelopeUrlVo();
        vo.setUrl("www.baidu.com?"+Math.random());
        return AjaxResult.success(vo);
    }
}
